package org.locker.device.controller.admin;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.DeviceUploadLogVo;
import org.locker.domain.bo.DeviceUploadLogBo;
import org.locker.service.IDeviceUploadLogService;
import org.locker.common.mybatis.core.page.TableDataInfo;

/**
 * 日志上传记录
 *
 * @author winnie
 * @date 2023-10-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/deviceUploadLog")
public class DeviceUploadLogController extends BaseController {

    private final IDeviceUploadLogService deviceUploadLogService;

    /**
     * 查询日志上传记录列表
     */
    @SaCheckPermission("device:deviceUploadLog:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceUploadLogVo> list(DeviceUploadLogBo bo, PageQuery pageQuery) {
        return deviceUploadLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出日志上传记录列表
     */
    @SaCheckPermission("device:deviceUploadLog:export")
    @Log(title = "日志上传记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceUploadLogBo bo, HttpServletResponse response) {
        List<DeviceUploadLogVo> list = deviceUploadLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "日志上传记录", DeviceUploadLogVo.class, response);
    }

    /**
     * 获取日志上传记录详细信息
     *
     * @param deviceUploadLogId 主键
     */
    @SaCheckPermission("device:deviceUploadLog:query")
    @GetMapping("/{deviceUploadLogId}")
    public R<DeviceUploadLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long deviceUploadLogId) {
        return R.ok(deviceUploadLogService.queryById(deviceUploadLogId));
    }

    /**
     * 新增日志上传记录
     */
    @SaCheckPermission("device:deviceUploadLog:add")
    @Log(title = "日志上传记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceUploadLogBo bo) {
        return toAjax(deviceUploadLogService.insertByBo(bo));
    }

    /**
     * 修改日志上传记录
     */
    @SaCheckPermission("device:deviceUploadLog:edit")
    @Log(title = "日志上传记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceUploadLogBo bo) {
        return toAjax(deviceUploadLogService.updateByBo(bo));
    }

    /**
     * 删除日志上传记录
     *
     * @param deviceUploadLogIds 主键串
     */
    @SaCheckPermission("device:deviceUploadLog:remove")
    @Log(title = "日志上传记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceUploadLogIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] deviceUploadLogIds) {
        return toAjax(deviceUploadLogService.deleteWithValidByIds(List.of(deviceUploadLogIds), true));
    }
}
